package org.thinker.point;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class PointServiceImpl implements PointService {
	
	private List<PointVO> list;
	
	public PointServiceImpl(){
		list = new ArrayList<PointVO>();
	}
	

	@Override
	public void create(PointVO vo) throws Exception {
		System.out.println("debug...create");
		
		if(list.contains(vo)){
			throw new Exception("Alreay Exist..");
		}
		
		list.add(vo);
		

	}

	@Override
	public PointVO read(String name) throws Exception {
		System.out.println("debug...read");
		PointVO target = new PointVO();
		target.setName(name);
		int idx = list.indexOf(target);
		return list.get(idx);
	}

	@Override
	public void update(PointVO vo) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("debug...update");
	}

	@Override
	public void delete(String name) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("debug...delete");
		
		PointVO vo  = new PointVO();
		vo.setName(name);
		
		list.remove(vo);
		
	}

	@Override
	public PointVO findNearestPoint(PointVO vo) throws Exception {
		// TODO Auto-generated method stub
		
		Collections.sort(list, new Comparator<PointVO>(){

			@Override
			public int compare(PointVO vo1, PointVO vo2) {
				
				return 0;
			}
			
		});
		
		return list.get(0);
	}


	@Override
	public List<PointVO> listAll() throws Exception {
		// TODO Auto-generated method stub
		return list;
	}

}
